
const userUtil = require('../../utils/user.js');

Page({
  data: {
    addrInfo: [],
    userinfo: {},
    checkStatus: true,
    status: "default" //状态分add,modify
  },

  onLoad: function (option) {

    var status = option.status;

    this.setData({
      status: option.status
    })

    //如果为修改状态可以进行值赋值
    if (status == 'modify') {
      var userinfo = JSON.parse(option.userinfo);
      this.setData({
        userinfo: userinfo,
      })
    }


  },



  // 选择微信自带地址
  addAddr: function () {
    var that = this;
    //选择地址
    wx.chooseLocation({
      success: function (res) {
        console.info(res);
        var userinfo = that.data.userinfo;
        userinfo.address = res.address;
        userinfo.realAddress = res.name;
        that.setData({
          userinfo: userinfo
        })
      },
    })
  },
  //地址是否默认
  onChange({
    detail
  }) {
    //获取用户信息
    var userinfo = this.data.userinfo;
    userinfo.status = detail;

    // 需要手动对用户的checked默认状态进行更新
    this.setData({
      userinfo: userinfo
    });
  },
  //添加地址
  addAddress: function () {
    var currentPage = getCurrentPages();
    var prevPage = currentPage[currentPage.length - 2]; //上一个页面

    var userinfo = this.data.userinfo;
    var flag = this.validateAddr(userinfo)
    if (!flag) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'error',
        duration: 1000
      })
    } else {
      userinfo.id = Date.now()
      userUtil.addAddress(userinfo)

      prevPage.setData({
        status: 'add',
        userinfo: userinfo
      })
      wx.navigateBack({
        delta: 1
      })
    }



  },
  //值修改实时赋值
  modifyValue: function (event) {
    var text = event.currentTarget.dataset.text; //文本信息
    var value = event.detail.value; //值
    var userinfo = this.data.userinfo; //获取当前的用户信息

    switch (text) {
      case "username":
        userinfo.username = value;
        break;
      case "phone":
        this.validatePhone(value)
        userinfo.phone = value;
        break;
      case "address":
        userinfo.address = value;
        break;
      case "realAddress":
        userinfo.realAddress = value;
        break;
      default:

    }
    
    this.setData({
      userinfo: userinfo
    })
  },


  //删除地址
  delAddress: function () {
    var currentPage = getCurrentPages();
    var prevPage = currentPage[currentPage.length - 2]; //上一个页面
    var userinfo = this.data.userinfo; //获取当前的用户信息

    userUtil.delAddress(userinfo)
    prevPage.setData({
      status: 'delete',
      userinfo: userinfo
    })
    wx.navigateBack({
      delta: 1
    })

  },
  //修改地址
  modifyAddress: function () {
    var currentPage = getCurrentPages();
    var prevPage = currentPage[currentPage.length - 2]; //上一个页面
    var userinfo = this.data.userinfo; //获取当前的用户信息
    var flag = this.validateAddr(userinfo)
    if (!flag) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'error',
        duration: 1000

      })
    } else {
      userUtil.updateAdress(userinfo)

      prevPage.setData({
        status: 'modify',
        userinfo: userinfo
      })
      wx.navigateBack({
        delta: 1
      })

    }

  },
  validateAddr(userinfo) {
    let { username, phone, address, realAddress } = userinfo
    if (username === undefined || phone === undefined || address === undefined || realAddress === undefined) {
      return false
    }
    return true
  },

  validatePhone(phone) {
    const regExp = new RegExp('^1[3456789]\\d{9}$');
    if (!regExp.test(phone)) {
      wx.showToast({
        title: '手机号码不正确',
        icon: 'error',
        duration: 1000
      })
    }
  }

})